﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using NTTDATA.BEENChina.ResultCertification.Checker;
using NTTDATA.BEENChina.ResultCertification.Logic;
using NTTDATA.BEENChina.ResultCertification.Result;
using Microsoft.ApplicationBlocks.Data;

namespace NTTDATA.BEENChina.ResultCertification.DataStore
{
    public class DataTarget :IDataTarget
    {

        public static string ConnectionString
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["ResultCertificationDB"];
            }
        }

        public int InsertOrders(IEnumerable<OrderData> orders)
        {
            string sql =
                " INSERT INTO [Orders] ([OrderID],[ServerCode],[OridReceiveDate],[State]) VALUES ( @OrderID , @ServerCode,@OridReceiveDate, @State)";

             if (orders == null  )
                return -1;

            int effactRows = 0;
            foreach (var orderData in orders)
            {
                bool exist = ExistOrder(orderData);

                if (!exist)
                {
                    SqlParameter[] sqlParameters =
                        {
                            new SqlParameter("@OrderID", orderData.OrderId),
                            new SqlParameter("@ServerCode", orderData.ServerCode),
                            //受番纳品时间
                            new SqlParameter("@OridReceiveDate", orderData.OridReceiveDate),
                            new SqlParameter("@State", EnumOrderState.受番取入),

                        };

                    effactRows += SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, sql, sqlParameters);
                }
            }


            return effactRows;
        }

        private bool ExistOrder(OrderData orderData)
        {
            string sql =
                " SELECT COUNT(*) FROM [Orders]  with(nolock) WHERE [OrderID] = @OrderID AND [ServerCode] =  @ServerCode "; 
          

                SqlParameter[] sqlParameters = 
                {
                    new SqlParameter("@OrderID", orderData.OrderId),
                    new SqlParameter("@ServerCode", orderData.ServerCode) 

                };

              string count =   SqlHelper.ExecuteScalar(ConnectionString, CommandType.Text, sql, sqlParameters).ToString();


            int rowCount = 0;

            int.TryParse(count,out rowCount);

            return rowCount > 0;

        }

        public List<OrderData> GetOrders(EnumOrderState state)
        {
            string sql = " SELECT [OrderID],[ServerCode], [State] FROM [Orders]  WHERE [State] = @State ";

            SqlParameter[] sqlParameters = 
                { 
                    new SqlParameter("@State",(int)state),

                };

            List<OrderData> orderDatas = new List<OrderData>();
            try
            {
                using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, System.Data.CommandType.Text, sql, sqlParameters))
                {
                    while (reader.Read())
                    {

                        orderDatas.Add(
                            new OrderData() {   OrderId = reader["OrderID"].ToString(),
                                                ServerCode = reader["ServerCode"].ToString()}
                        );
                    }

                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                Log.Console.Error("DataTarget.GetOrders", ex);
            }

            return orderDatas;
        }

        public int UpdateOrderStates(string serverCode,  string orderId, EnumOrderState state)
        {
            string sql = " UPDATE [Orders]   SET [State] = @State " +
                        " WHERE [OrderID] = @OrderID AND [ServerCode] = @ServerCode";

            SqlParameter[] sqlParameters = 
                {
                    new SqlParameter("@OrderID",  orderId),
                    new SqlParameter("@ServerCode", serverCode),
                    new SqlParameter("@State",(int)state),

                };

           return SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, sql, sqlParameters);
        }

        public int RemoveOrders()
        {
            string sql =
              " DELETE FROM [Orders] ";

            return  SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, sql);

        }

        /// <summary>
        /// 录入结果保存到DB
        /// </summary>
        /// <param name="childImage"></param>
        /// <returns></returns>
        public int InsertChildImage(ChildImage childImage)
        {  
            string sql = "   INSERT INTO [ChildImages] " +
                         "           ([OrderID],[ImageID],[FormID],[FormName]  " +
                         "           ,[CustomerID] ,[FieldID]  ,[FieldName] " +
                         "           ,[CategoryID] ,[CategoryName] " +
                         "           ,[StoreTableMame] " +
                         "           ,[ABJobId]   " +
                         "           ,[AOpID],[AResult] ,[AEntryCommentID]  " +
                         "           ,[BOpID],[BResult] ,[BEntryCommentID] " +
                         "           ,[CJobId]   " +
                         "           ,[COpID],[CResult] ,[CEntryCommentID] " +
                         "           ,[DJobId]   " +
                         "           ,[DOpID],[DResult] ,[DEntryCommentID] " +
                         "           ,[EJobId]   " +
                         "           ,[EOpID],[EResult]  " +
                         "           ,[FinalResult] ,[FinalEntryCommentID]" +
                         "           ,[VerfityItemID] " +
                         "           ,[EntryRule] "+
                         "           ,[VerfityProperties] " +
                         ") " +
                         "     VALUES " +
                         "           ( @OrderID,@ImageID,@FormID,@FormName " +
                         "           ,@CustomerID ,@FieldID  ,@FieldName " +
                         "           ,@CategoryID ,@CategoryName " +
                         "           ,@StoreTableMame " +
                         "           ,@ABJobId " +
                         "           ,@AOpID,@AResult ,@AEntryCommentID  " +
                         "           ,@BOpID,@BResult ,@BEntryCommentID " +
                         "           ,@CJobId " +
                         "           ,@COpID,@CResult ,@CEntryCommentID " +
                         "           ,@DJobId " +
                         "           ,@DOpID,@DResult ,@DEntryCommentID " +
                         "           ,@EJobId " +
                         "           ,@EOpID,@EResult  " +
                         "           ,@FinalResult ,@FinalEntryCommentID" +
                         "           ,@VerfityItemID" +
                         "           ,@EntryRule " +
                         "           ,@VerfityProperties " +
                         ") ";
            
            return SaveImageObject(sql, childImage);            
        }

        public int SaveVerfityConfirmResultAndOpid(ChildImage childImage)
        {
            string sql = "  UPDATE [ChildImages]  " +
                        "    SET  [AOpID] = @AOpID	 " +
                        "       , [BOpID]= @BOpID	 " +
                        "       , [COpID] = @COpID	 " +
                        "       , [DOpID] = @DOpID	 " +
                        "       , [EOpID] = @EOpID	 " +
                        "       ,[VerfityTime] = @VerfityTime    " +
                        "       ,[VerfityResult] = @VerfityResult   " +
                        "       ,[VerfityComment] = @VerfityComment " +
                        "       ,[MissType] = @MissType " +
                        "       ,[MissWord] = @MissWord " +
                        "       ,[VerfityProperties]  = @VerfityProperties " +
                        "    WHERE ID = @id ";

       
            if (childImage == null)
                return -1;

            int effactRows = 0;

            int aResultCount = childImage.OperatorEntryResults.Count(t => t.Rank.Equals(EnumRank.A));
            int bResultCount = childImage.OperatorEntryResults.Count(t => t.Rank.Equals(EnumRank.B));
            int cResultCount = childImage.OperatorEntryResults.Count(t => t.Rank.Equals(EnumRank.C));
            int dResultCount = childImage.OperatorEntryResults.Count(t => t.Rank.Equals(EnumRank.D));
            int eResultCount = childImage.OperatorEntryResults.Count(t => t.Rank.Equals(EnumRank.E)); 

            OpEntryResult aResult = null, bResult = null, cResult = null, dResult = null, eResult = null, fResult = null;

            if (aResultCount > 0)
            {
                aResult = childImage.OperatorEntryResults.Single(t => t.Rank.Equals(EnumRank.A));
            }
            if (bResultCount > 0)
            {
                bResult = childImage.OperatorEntryResults.Single(t => t.Rank.Equals(EnumRank.B));
            }
            if (cResultCount > 0)
            {
                cResult = childImage.OperatorEntryResults.Single(t => t.Rank.Equals(EnumRank.C));
            }
            if (dResultCount > 0)
            {
                dResult = childImage.OperatorEntryResults.Single(t => t.Rank.Equals(EnumRank.D));
            }
            if (eResultCount > 0)
            {
                eResult = childImage.OperatorEntryResults.Single(t => t.Rank.Equals(EnumRank.E));
            }
           

            try
            {
                SqlParameter[] sqlParameters = 
                    {                     
                        new SqlParameter("@AOpID" ,  DBNull.Value  ),
                        new SqlParameter("@BOpID" ,DBNull.Value),
                        new SqlParameter("@COpID" ,DBNull.Value),
                        new SqlParameter("@DOpID" ,DBNull.Value), 
                        new SqlParameter("@EOpID" ,DBNull.Value) ,
                        new SqlParameter("@VerfityTime", childImage.VerfityConfirmResult.VerfityTime),
                        new SqlParameter("@VerfityResult", childImage.VerfityConfirmResult.Result),
                        new SqlParameter("@VerfityComment", childImage.VerfityConfirmResult.VerfityComment),
                        new SqlParameter("@MissType", childImage.VerfityConfirmResult.MissType),
                        new SqlParameter("@MissWord", childImage.VerfityConfirmResult.MissWord),
                        new SqlParameter("@id", childImage.SeqNo),
                        new SqlParameter("@VerfityProperties", DBNull.Value)
                    };

                if (aResultCount > 0 && aResult != null)
                {
                    sqlParameters.Single(t => t.ParameterName == "@AOpID").Value = aResult.OperatorId;
                }

                if (bResultCount > 0 && bResult != null)
                {
                    sqlParameters.Single(t => t.ParameterName == "@BOpID").Value = bResult.OperatorId;
                }

                if (cResultCount > 0 && cResult != null)
                {
                    sqlParameters.Single(t => t.ParameterName == "@COpID").Value = cResult.OperatorId;
                }

                if (dResultCount > 0 && dResult != null)
                {
                    sqlParameters.Single(t => t.ParameterName == "@DOpID").Value = dResult.OperatorId; 
                }

                if (eResultCount > 0 && eResult != null)
                {
                    sqlParameters.Single(t => t.ParameterName == "@EOpID").Value = eResult.OperatorId;
                }

                if (childImage.Reason != null)
                {
                    string reasonXml = new Util.Serializer().SerializeXml(typeof(VerfityProperties), childImage.Reason);
                    sqlParameters.Single(t => t.ParameterName == "@VerfityProperties").Value = reasonXml;
                }

                effactRows = SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, sql, sqlParameters);

            }
            catch (Exception ex)
            {
                Log.Console.Error("DataTarget.SaveVerfityConfirmResultAndOpid", ex);
            }

            return effactRows;
        }

        private int SaveImageObject(string sql, ChildImage childImage)
        {
            if (childImage == null)
                return -1;

            int effactRows = 0;

            int aResultCount = childImage.OperatorEntryResults.Count(t => t.Rank.Equals(EnumRank.A));
            int bResultCount = childImage.OperatorEntryResults.Count(t => t.Rank.Equals(EnumRank.B));
            int cResultCount = childImage.OperatorEntryResults.Count(t => t.Rank.Equals(EnumRank.C));
            int dResultCount = childImage.OperatorEntryResults.Count(t => t.Rank.Equals(EnumRank.D));
            int eResultCount = childImage.OperatorEntryResults.Count(t => t.Rank.Equals(EnumRank.E));
            int fResultCount = childImage.OperatorEntryResults.Count(t => t.Rank.Equals(EnumRank.Final));

            OpEntryResult aResult = null, bResult = null, cResult = null, dResult = null, eResult = null, fResult = null;

            if (aResultCount > 0)
            {
                aResult = childImage.OperatorEntryResults.Single(t => t.Rank.Equals(EnumRank.A));
            }
            if (bResultCount > 0)
            {
                bResult = childImage.OperatorEntryResults.Single(t => t.Rank.Equals(EnumRank.B));
            }
            if (cResultCount > 0)
            {
                cResult = childImage.OperatorEntryResults.Single(t => t.Rank.Equals(EnumRank.C));
            }
            if (dResultCount > 0)
            {
                dResult = childImage.OperatorEntryResults.Single(t => t.Rank.Equals(EnumRank.D));
            }
            if (eResultCount > 0)
            {
                eResult = childImage.OperatorEntryResults.Single(t => t.Rank.Equals(EnumRank.E));
            }
            if (fResultCount > 0)
            {
                fResult = childImage.OperatorEntryResults.Single(t => t.Rank.Equals(EnumRank.Final));
            }

           

            try
            {
                SqlParameter[] sqlParameters = 
                    {                        
                        new SqlParameter("@OrderID", childImage.OrderId), 
                        new SqlParameter("@ImageID", childImage.ImageId),
                        new SqlParameter("@FormID", childImage.FormId), 
                        new SqlParameter("@FormName", childImage.FormName), 
                        new SqlParameter("@CustomerID",childImage.CustomerId),
                        new SqlParameter("@FieldID"  ,childImage.FieldId),
                        new SqlParameter("@FieldName",childImage.FieldName),  
                        new SqlParameter("@CategoryID" ,childImage.CategoryId),
                        new SqlParameter("@CategoryName",childImage.CategoryName),  

                        new SqlParameter("@StoreTableMame" ,  childImage.StoreTableName  ),

                        new SqlParameter("@ABJobId" ,  DBNull.Value  ),
                        new SqlParameter("@AOpID" ,  DBNull.Value  ),
                        new SqlParameter("@AResult" ,DBNull.Value),
                        new SqlParameter("@AEntryCommentID" ,DBNull.Value), 

                        new SqlParameter("@BOpID" ,DBNull.Value),
                        new SqlParameter("@BResult" ,DBNull.Value), 
                        new SqlParameter("@BEntryCommentID" ,DBNull.Value),  

                        new SqlParameter("@CJobId" ,  DBNull.Value  ),
                        new SqlParameter("@COpID" ,DBNull.Value),
                        new SqlParameter("@CResult" ,DBNull.Value),
                        new SqlParameter("@CEntryCommentID" ,DBNull.Value),   

                        new SqlParameter("@DJobId" ,  DBNull.Value  ),
                        new SqlParameter("@DOpID" ,DBNull.Value),
                        new SqlParameter("@DResult" ,DBNull.Value),
                        new SqlParameter("@DEntryCommentID" ,DBNull.Value),   

                        new SqlParameter("@EJobId" ,  DBNull.Value  ),
                        new SqlParameter("@EOpID" ,DBNull.Value),
                        new SqlParameter("@EResult" ,DBNull.Value),  

                        new SqlParameter("@FinalResult" ,DBNull.Value), 
                        new SqlParameter("@FinalEntryCommentID" ,DBNull.Value), 


                        new SqlParameter("@VerfityItemID" ,childImage.Reason.VerfityItemId),

                        new SqlParameter("@EntryRule" ,childImage.EntryRule),

                         new SqlParameter("@VerfityProperties" ,  DBNull.Value ),
                    };

                if (aResultCount > 0 && aResult != null)
                {
                    sqlParameters.Single(t => t.ParameterName == "@ABJobId").Value = aResult.JobId;
                    sqlParameters.Single(t => t.ParameterName == "@AOpID").Value = aResult.OperatorId;
                    sqlParameters.Single(t => t.ParameterName == "@AResult").Value = aResult.EntryResult;
                    sqlParameters.Single(t => t.ParameterName == "@AEntryCommentID").Value = aResult.EntryCommon;
                }

                if (bResultCount > 0 && bResult != null)
                {
                    sqlParameters.Single(t => t.ParameterName == "@BOpID").Value = bResult.OperatorId;
                    sqlParameters.Single(t => t.ParameterName == "@BResult").Value = bResult.EntryResult;
                    sqlParameters.Single(t => t.ParameterName == "@BEntryCommentID").Value = bResult.EntryCommon;
                }

                if (cResultCount > 0 && cResult != null)
                {
                    sqlParameters.Single(t => t.ParameterName == "@CJobId").Value = cResult.JobId;
                    sqlParameters.Single(t => t.ParameterName == "@COpID").Value = cResult.OperatorId;
                    sqlParameters.Single(t => t.ParameterName == "@CResult").Value = cResult.EntryResult;
                    sqlParameters.Single(t => t.ParameterName == "@CEntryCommentID").Value = cResult.EntryCommon;
                }

                if (dResultCount > 0 && dResult != null)
                {
                    sqlParameters.Single(t => t.ParameterName == "@DJobId").Value = dResult.JobId;
                    sqlParameters.Single(t => t.ParameterName == "@DOpID").Value = dResult.OperatorId;
                    sqlParameters.Single(t => t.ParameterName == "@DResult").Value = dResult.EntryResult;
                    sqlParameters.Single(t => t.ParameterName == "@DEntryCommentID").Value = dResult.EntryCommon;
                }

                if (eResultCount > 0 && eResult != null)
                {
                    sqlParameters.Single(t => t.ParameterName == "@EJobId").Value = eResult.JobId;
                    sqlParameters.Single(t => t.ParameterName == "@EOpID").Value = eResult.OperatorId;
                    sqlParameters.Single(t => t.ParameterName == "@EResult").Value = eResult.EntryResult;
                }

                if (fResultCount > 0 && fResult != null)
                {
                    sqlParameters.Single(t => t.ParameterName == "@FinalResult").Value = fResult.EntryResult;
                    sqlParameters.Single(t => t.ParameterName == "@FinalEntryCommentID").Value = fResult.EntryCommon;
                }

                if (childImage.Reason != null)
                {
                    string reasonXml = new Util.Serializer().SerializeXml(typeof(VerfityProperties), childImage.Reason);
                    sqlParameters.Single(t => t.ParameterName == "@VerfityProperties").Value = reasonXml;
                }

                effactRows = SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, sql, sqlParameters);

            }
            catch (Exception ex)
            {
                Log.Console.Error("DataTarget.InsertChildImage", ex);
            }

            return effactRows;            
        }

        public  bool ExistChildImages(string orderid)
         {
             string sql =
                " SELECT COUNT(*) FROM [ChildImages]  with(nolock) WHERE [OrderID] = @OrderID  ";


             SqlParameter[] sqlParameters = 
                {
                    new SqlParameter("@OrderID", orderid) 
                };

             string count = SqlHelper.ExecuteScalar(ConnectionString, CommandType.Text, sql, sqlParameters).ToString(); 

             int rowCount = 0;

             int.TryParse(count, out rowCount);

             return rowCount > 0;
         }

        public int DeleteChildImages(string orderid)
        {
            int effactRows = 0;
            try
            {

           
                string sql = " DELETE FROM [ChildImages]  WHERE OrderID = @OrderID";

                SqlParameter[] sqlParameters = 
                        {                     
                            new SqlParameter("@OrderID", orderid)  
                        };


               effactRows = SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.Text,  sql, sqlParameters); 
            }
            catch (Exception ex)
            {

                Log.Console.Error("DataTarget.DeleteChildImages", ex);
            }

            return effactRows;
        }

        /// <summary>
        /// 供测试使用
        /// </summary>
        /// <returns></returns>
        public List<ChildImage> GetTestChildImageObjects()
        {

            string sql = "SELECT    " +
                         "           [OrderID],[ImageID],[FormID] " +
                         "           ,[CustomerID] ,[FieldID]  ,[FieldName] " +
                         "           ,[CategoryID] ,[CategoryName] " +
                         "           ,[AResult] ,[AEntryCommentID]  " +
                         "           ,[BResult] ,[BEntryCommentID] " +
                         "           ,[CResult] ,[CEntryCommentID] " +
                         "           ,[DResult] ,[DEntryCommentID] " +
                         "           ,[EResult]  " +
                         "           ,[FinalResult] ,[FinalEntryCommentID]  " +

                        " FROM [store_test] ";
             
             

            List<ChildImage> imagesChilds = new List<ChildImage>();
            try
            {
                using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, System.Data.CommandType.Text, sql))
                {
                    while (reader.Read())
                    {


                        ChildImage child = new ChildImage()
                        {

                            OrderId = reader["OrderID"].ToString(),
                            ImageId = reader["ImageID"].ToString(),
                            FormId = reader["FormID"].ToString(),
                            CustomerId = reader["CustomerID"].ToString(),
                            FieldId = reader["FieldID"].ToString(),
                            FieldName = reader["FieldName"].ToString(),
                            CategoryId = reader["CategoryID"].ToString(),
                            CategoryName = reader["CategoryName"].ToString(),


                        };

                        child.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.A, EntryResult = reader["AResult"].ToString(), EntryCommon = reader["AEntryCommentID"].ToString() });
                        child.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.B, EntryResult = reader["BResult"].ToString(), EntryCommon = reader["BEntryCommentID"].ToString() });
                        child.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.C, EntryResult = reader["CResult"].ToString(), EntryCommon = reader["CEntryCommentID"].ToString() });
                        child.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.D, EntryResult = reader["DResult"].ToString(), EntryCommon = reader["DEntryCommentID"].ToString() });
                        child.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.E, EntryResult = reader["EResult"].ToString(), EntryCommon = reader["EResult"].ToString() });
                        child.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.Final, EntryResult = reader["FinalResult"].ToString(), EntryCommon = reader["FinalEntryCommentID"].ToString() });


                        imagesChilds.Add(child);
                    }

                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                Log.Console.Error("DataTarget.GetTestChildImageObjects", ex);
            }

 
            return imagesChilds;
        }

        /// <summary>
        /// 供测试使用
        /// </summary>
        /// <returns></returns>
        public List<ChildImage> GetTestChildImageObjects(string categoryId)
        {

            string sql = "SELECT    " +
                         "           [OrderID],[ImageID],[FormID] " +
                         "           ,[CustomerID] ,[FieldID]  ,[FieldName] " +
                         "           ,[CategoryID] ,[CategoryName] " +
                         "           ,[AResult] ,[AEntryCommentID]  " +
                         "           ,[BResult] ,[BEntryCommentID] " +
                         "           ,[CResult] ,[CEntryCommentID] " +
                         "           ,[DResult] ,[DEntryCommentID] " +
                         "           ,[EResult]  " +
                         "           ,[FinalResult] ,[FinalEntryCommentID]  " +

                        " FROM [store_test] WHERE  [CategoryID]  = @CategoryID ";



            SqlParameter[] sqlParameters = 
                { 
                    new SqlParameter("@CategoryID", categoryId),

                };

            List<ChildImage> imagesChilds = new List<ChildImage>();
            try
            {
                using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, System.Data.CommandType.Text, sql, sqlParameters))
                {
                    while (reader.Read())
                    {


                        ChildImage child = new ChildImage()
                        {

                            OrderId = reader["OrderID"].ToString(),
                            ImageId = reader["ImageID"].ToString(),
                            FormId = reader["FormID"].ToString(),
                            CustomerId = reader["CustomerID"].ToString(),
                            FieldId = reader["FieldID"].ToString(),
                            FieldName = reader["FieldName"].ToString(),
                            CategoryId = reader["CategoryID"].ToString(),
                            CategoryName = reader["CategoryName"].ToString(),
                            

                        };

                        child.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.A, EntryResult = reader["AResult"].ToString(), EntryCommon = reader["AEntryCommentID"].ToString() });
                        child.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.B, EntryResult = reader["BResult"].ToString(), EntryCommon = reader["BEntryCommentID"].ToString() });
                        child.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.C, EntryResult = reader["CResult"].ToString(), EntryCommon = reader["CEntryCommentID"].ToString() });
                        child.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.D, EntryResult = reader["DResult"].ToString(), EntryCommon = reader["DEntryCommentID"].ToString() });
                        child.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.E, EntryResult = reader["EResult"].ToString() });
                        child.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.Final, EntryResult = reader["FinalResult"].ToString(), EntryCommon = reader["FinalEntryCommentID"].ToString() });


                        imagesChilds.Add(child);
                    }

                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                Log.Console.Error("DataTarget.GetTestChildImageObjects", ex);
            }


            return imagesChilds;
        }

        public ChildImage GetTestChildImageObject(string imageId)
        {

            string sql = "SELECT    " +
                         "           [OrderID],[ImageID],[FormID] " +
                         "           ,[CustomerID] ,[FieldID]  ,[FieldName] " +
                         "           ,[CategoryID] ,[CategoryName] " +
                         "           ,[AResult] ,[AEntryCommentID]  " +
                         "           ,[BResult] ,[BEntryCommentID] " +
                         "           ,[CResult] ,[CEntryCommentID] " +
                         "           ,[DResult] ,[DEntryCommentID] " +
                         "           ,[EResult]  " +
                         "           ,[FinalResult] ,[FinalEntryCommentID]  " +

                        " FROM [store_test] WHERE  [ImageID]  = @ImageID ";



            SqlParameter[] sqlParameters = 
                { 
                    new SqlParameter("@ImageID", imageId),

                };

            ChildImage child = null;
            try
            {
                using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, System.Data.CommandType.Text, sql, sqlParameters))
                {
                    while (reader.Read())
                    {


                       child = new ChildImage()
                        {

                            OrderId = reader["OrderID"].ToString(),
                            ImageId = reader["ImageID"].ToString(),
                            FormId = reader["FormID"].ToString(),
                            CustomerId = reader["CustomerID"].ToString(),
                            FieldId = reader["FieldID"].ToString(),
                            FieldName = reader["FieldName"].ToString(),
                            CategoryId = reader["CategoryID"].ToString(),
                            CategoryName = reader["CategoryName"].ToString(),


                        };

                        child.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.A, EntryResult = reader["AResult"].ToString(), EntryCommon = reader["AEntryCommentID"].ToString() });
                        child.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.B, EntryResult = reader["BResult"].ToString(), EntryCommon = reader["BEntryCommentID"].ToString() });
                        child.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.C, EntryResult = reader["CResult"].ToString(), EntryCommon = reader["CEntryCommentID"].ToString() });
                        child.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.D, EntryResult = reader["DResult"].ToString(), EntryCommon = reader["DEntryCommentID"].ToString() });
                        child.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.E, EntryResult = reader["EResult"].ToString(), EntryCommon = reader["EResult"].ToString() });
                        child.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.Final, EntryResult = reader["FinalResult"].ToString(), EntryCommon = reader["FinalEntryCommentID"].ToString() });


                        
                    }

                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                Log.Console.Error("DataTarget.GetTestChildImageObject", ex);
            }


            return child;
        }

        /// <summary>
        /// for test
        /// </summary>
        /// <param name="imageId"></param>
        /// <returns></returns>
        public ChildImage GetChildImageObject(string imageId)
        {

            string sql = "SELECT    " +
                         "           [OrderID],[ImageID],[FormID] " +
                         "           ,[CustomerID] ,[FieldID]  ,[FieldName] " +
                         "           ,[CategoryID] ,[CategoryName] " +
                         "           ,[AResult] ,[AEntryCommentID]  " +
                         "           ,[BResult] ,[BEntryCommentID] " +
                         "           ,[CResult] ,[CEntryCommentID] " +
                         "           ,[DResult] ,[DEntryCommentID] " +
                         "           ,[EResult]  " +
                         "           ,[FinalResult] ,[FinalEntryCommentID]  " +

                        " FROM [ChildImages] with(nolock) WHERE  [ImageID]  = @ImageID ";



            SqlParameter[] sqlParameters = 
                { 
                    new SqlParameter("@ImageID", imageId),

                };

           ChildImage imagesChild  = null;
            try
            {
                using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, System.Data.CommandType.Text, sql, sqlParameters))
                {
                    while (reader.Read())
                    {


                        imagesChild = new ChildImage()
                        {

                            OrderId = reader["OrderID"].ToString(),
                            ImageId = reader["ImageID"].ToString(),
                            FormId = reader["FormID"].ToString(),
                            CustomerId = reader["CustomerID"].ToString(),
                            FieldId = reader["FieldID"].ToString(),
                            FieldName = reader["FieldName"].ToString(),
                            CategoryId = reader["CategoryID"].ToString(),
                            CategoryName = reader["CategoryName"].ToString(),


                        };

                        imagesChild.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.A, EntryResult = reader["AResult"].ToString(), EntryCommon = reader["AEntryCommentID"].ToString() });
                        imagesChild.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.B, EntryResult = reader["BResult"].ToString(), EntryCommon = reader["BEntryCommentID"].ToString() });
                        imagesChild.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.C, EntryResult = reader["CResult"].ToString(), EntryCommon = reader["CEntryCommentID"].ToString() });
                        imagesChild.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.D, EntryResult = reader["DResult"].ToString(), EntryCommon = reader["DEntryCommentID"].ToString() });
                        imagesChild.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.E, EntryResult = reader["EResult"].ToString(), EntryCommon = reader["EResult"].ToString() });
                        imagesChild.OperatorEntryResults.Add(new OpEntryResult() { Rank = EnumRank.Final, EntryResult = reader["FinalResult"].ToString(), EntryCommon = reader["FinalEntryCommentID"].ToString() });


                        
                    }

                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                Log.Console.Error("DataTarget.GetChildImageObject", ex);
            }


            return imagesChild;
        }
    }
}
